package luminus.origin

import luminus.origin.utils.Configurations
import luminus.origin.utils.Configurations.config
import org.bukkit.event.player.PlayerJoinEvent
import taboolib.common.LifeCycle
import taboolib.common.LifeCycleTask
import taboolib.common.platform.Plugin
import taboolib.common.platform.event.SubscribeEvent
import taboolib.common.platform.function.info
import taboolib.module.lang.Language
import taboolib.module.lang.event.PlayerSelectLocaleEvent
import taboolib.module.lang.event.SystemSelectLocaleEvent
import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.*


object Luminus : Plugin() {


    override fun onEnable() {
        info("Start enabling Luminus")
        Configurations.reload()
        Language.enableSimpleComponent = true
    }

    @SubscribeEvent
    fun lang(event: PlayerSelectLocaleEvent) {
        event.locale = config.getString("Lang", "zh_CN")!!
    }

    @SubscribeEvent
    fun lang(event: SystemSelectLocaleEvent) {
        event.locale = config.getString("Lang", "zh_CN")!!
    }

    init {
        System.setProperty("taboolib.disable-ssl-verify", "true")
        HttpsURLConnection.setDefaultHostnameVerifier { _: String?, _: SSLSession? -> true }
        HttpsURLConnection.setDefaultSSLSocketFactory(trustAllCertificates())
    }

    @Throws(Exception::class)
    private fun trustAllCertificates(): SSLSocketFactory {
        val sslContext = SSLContext.getInstance("TLS")
        sslContext.init(null, arrayOf<TrustManager>(object : X509TrustManager {
            override fun checkClientTrusted(chain: Array<X509Certificate?>?, authType: String?) {}
            override fun checkServerTrusted(chain: Array<X509Certificate?>?, authType: String?) {}
            override fun getAcceptedIssuers(): Array<X509Certificate?> {
                return arrayOfNulls(0)
            }
        }), SecureRandom())
        return sslContext.socketFactory
    }
}